//
//  LFBShoppingCartManager.m
//  loveFreshBeen
//
//  Created by Feng on 2017/7/9.
//  Copyright © 2017年 feng. All rights reserved.
//

#import "LFBShoppingCartManager.h"
#import "LFBShoppingCartModel.h"

@implementation LFBShoppingCartManager

+ (instancetype)sharedManager
{
    static LFBShoppingCartManager *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[LFBShoppingCartManager alloc] init];
        
    });
    
    return instance;
}

/**
 增加购物商品
 
 @param model 商品模型
 @param isFirstTime 是否第一次增入
 */
- (void)addShoppingCartProductWithProdunctModel:(LFBShoppingCartModel *)model andIsFirstTime:(BOOL)isFirstTime
{
    __block NSInteger produntCount = 0;
    
    [self.shoppingCartArray enumerateObjectsUsingBlock:^(LFBShoppingCartModel * _Nonnull cartModel, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if(model.product_id == cartModel.product_id)
        {
            if(!isFirstTime)
            {
                if(cartModel.buyNums < cartModel.store_nums)
                {
                    //购买数+1
                    cartModel.buyNums++;
                    //总数+1
                    self.sumNums++;
                }
            }
            produntCount++;
        }
    }];

    //第一次从网络加载数据,将商品模型增入到数组,此时数量都应该为0
    if(produntCount == 0)
    {
        if(isFirstTime)
        {
            //将模型增入数组
            model.buyNums = 0;
            [self.shoppingCartArray addObject:model];
        }
        else
        {
            //将模型增入数组
            model.buyNums = 1;
            [self.shoppingCartArray addObject:model];
            
            //总数+1
            self.sumNums++;
        }
    }
}

/**
 减少购物商品
 
 @param model 商品
 */
- (void)subShoppingCartProductWithProdunctModel:(LFBShoppingCartModel *)model
{
    [self.shoppingCartArray enumerateObjectsUsingBlock:^(LFBShoppingCartModel * _Nonnull cartModel, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if(model.product_id == cartModel.product_id)
        {
            if(cartModel.buyNums > 0)
            {
                //购买数-1
                cartModel.buyNums--;
                //总数-1
                self.sumNums--;
            }
        }
        
        if(cartModel.buyNums == 0)
        {
            [self.shoppingCartArray removeObject:model];
        }
        
    }];
}

/**
 根据商品id返回数量
 
 @param productID 商品id
 @return 商品数量
 */
- (NSInteger)productCountWithShoppingCartModelID:(NSString *)productID
{
    __block NSInteger produntCount = 0;
    
    [self.shoppingCartArray enumerateObjectsUsingBlock:^(LFBShoppingCartModel * _Nonnull cartModel, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if(productID == cartModel.product_id)
        {
            produntCount = cartModel.buyNums;
        }
    }];
    
    return produntCount;
}

/**
 根据商品id返回模型
 
 @param productID 商品id
 @return 模型
 */
- (LFBShoppingCartModel *)shoppingCartModelWithModelID:(NSString *)productID
{
    __block LFBShoppingCartModel *model = [LFBShoppingCartModel new];
    
    [self.shoppingCartArray enumerateObjectsUsingBlock:^(LFBShoppingCartModel * _Nonnull cartModel, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if(productID == cartModel.product_id)
        {
            model = cartModel;
        }
    }];
    
    return model;
}

/**
 懒加载

 @return shoppingCartArray
 */
- (NSMutableArray *)shoppingCartArray
{
    if(_shoppingCartArray == nil)
    {
        _shoppingCartArray = [NSMutableArray array];
    }
    
    return _shoppingCartArray;
}

@end
